package com.shop.controller;

import com.shop.annotation.RedisLock;
import com.shop.annotation.RequireRoles;
import com.shop.entity.CommonUser;
import com.shop.entity.MerchantMerchant;
import com.shop.model.PageParam;
import com.shop.model.vo.Rsp;
import com.shop.service.MerchantService;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @BelongsProject: shop
 * @BelongsPackage: com.shop.controller
 * @Author: 尹标
 * @CreateTime: 2020-11-25 10:15
 * @Description: 商户信息接口
 */
@Validated
@RestController
@RequestMapping("merchant")
@AllArgsConstructor
public class MerchantController {

    private final MerchantService merchantService;

    /**
     * 获取商户列表
     */
    //@RequireRoles(roles = "platform")
    @PostMapping("searchMerchantPage")
    public Rsp searchMerchantPage(@RequestBody PageParam<MerchantMerchant> pageParam) {
        return Rsp.success(merchantService.searchMerchantPage(pageParam));
    }

    /**
     * 修改商户状态开关
     */
    //@RequireRoles(roles = "platform")
    @PostMapping("changeStatus")
    public Rsp changeStatus(@RequestBody MerchantMerchant merchantMerchant) {
        return merchantService.changeStatus(merchantMerchant);
    }

    @RequireRoles(roles = "merchant")
    @GetMapping("getMerchantInfoByLoginId")
    public Rsp getMerchantInfoByLoginId(@RequestHeader("merchantId") Integer merchantId) {
        return new Rsp(merchantService.getMerchantInfoByLoginId(merchantId) != null ? 200 : 500, "", merchantService.getMerchantInfoByLoginId(merchantId));
    }

    /**
     * 修改商户审核状态开关
     */
    @RequireRoles(roles = "platform")
    @PostMapping("changeExamineStatus")
    public Rsp changeExamineStatus(@RequestBody MerchantMerchant merchantMerchant) {
        return merchantService.changeExamineStatus(merchantMerchant);
    }

    /**
     * 根据id逻辑删除商户
     */
    @RequireRoles(roles = "platform")
    @PostMapping("deleteOne")
    public Rsp deleteOne(@RequestBody MerchantMerchant merchantMerchant) {
        return merchantService.deleteOne(merchantMerchant);
    }


    @RequireRoles(roles = "merchant")
    @RequestMapping("merchantImageUpload")
    public Rsp merchantImageUpload(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        System.out.println(multipartFile.getOriginalFilename());
        String path = "D:\\Download" + File.separator + multipartFile.getOriginalFilename();
        multipartFile.transferTo(new File(path));
        return null;
    }


    /**
     * 添加商户账号
     *
     * @param merchant
     * @return
     */
    //@RequireRoles(roles = "platform")
    @PostMapping("insertOne")
    public Rsp insertOne(@RequestBody MerchantMerchant merchant) {
        return merchantService.insertOne(merchant);
    }

    /**
     * 测试分布式锁
     */
    @RedisLock(lockName = "testLock", key = "#user.account")
    @PostMapping("testLock")
    public Rsp testLock(CommonUser user) throws InterruptedException {
        Thread.sleep(12000L);
        int a = 1 / 0;
        return Rsp.success(null).msg("运行完毕");
    }
}
